import { useEffect } from 'react';
import { useModel } from 'umi';

/**
 * 设置面包屑
 * @param {String} breadcrumbText
 */
export default function useBreadcrumb(breadcrumbText = '') {
  const { breadcrumb, setBreadcrumb } = useModel(
    'global',
    ({ breadcrumb, setBreadcrumb }) => ({
      breadcrumb,
      setBreadcrumb,
    }),
  );
  useEffect(() => {
    if (breadcrumbText) {
      setBreadcrumb(breadcrumbText);
    }
    return () => {
      setBreadcrumb('');
    };
  }, [breadcrumbText]);

  return {
    breadcrumb,
    setBreadcrumb,
  };
}
